package wnhz.com.business.bindphone;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.R;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.groupshop.bean.InfoBean;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.ConfirmUtils;
import wnhz.com.business.uitls.CountDownTimerUtils;
import wnhz.com.business.uitls.Url;

/**
 * Created by lizhenglong on 2017-10-24 0024.
 * 更换绑定手机
 */

public class BindPhone2Activtity extends MyBaseActivity {
    private static final String TAG = "RegisterGetCodeActivtity";
    @BindView(R.id.rl_register_back)//返回
    RelativeLayout rl_registerrack;
    @BindView(R.id.tv_bindphone1_phone)//手机号码
    TextView tv_bindphone1_phone;
    @BindView(R.id.ed_bindphone2_code)//输入验证码
    EditText ed_bindphone2_code;
    @BindView(R.id.tv_bindphone2_getcode)//获取验证码
    TextView tv_bindphone2_getcode;
    private String phone = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bindphone2);
        ButterKnife.bind(this);
        phone = getIntent().getStringExtra("phone");
        tv_bindphone1_phone.setText(phone);
        bindPhonegetcode();
    }

    @OnClick({R.id.tv_bindphone2_getcode,R.id.rl_register_back , R.id.btn_bindphone1})
    void clickBindPhone2(View view) {
        switch (view.getId()) {
            case R.id.rl_register_back:
                finish();
                break;
            case R.id.tv_bindphone2_getcode://点击获取验证码
                if (TextUtils.isEmpty(phone)){
                    MyToast("请输入手机号码");
                    return;
                }
                if (!ConfirmUtils.isMobile(phone)){
                    MyToast("手机号码格式不正确");
                    return;
                }
                bindPhonegetcode();
                break;
            case R.id.btn_bindphone1://确定按钮_验证验证码
                if (TextUtils.isEmpty(ed_bindphone2_code.getText().toString())){
                    MyToast("请输入验证码");
                    return;
                }
                checkcode();
                break;
        }
    }

    private void checkcode() {

        /**验证验证码
         @param Int    mobile    手机号   （加密）
         @param Str    code      验证码
         @param Str    token     type为 4,5,6 时必需
         @param Int    type      3 忘记密码 4 更换手机号(第一次使用,第二次请求更换手机号接口) 5 修改交易密码  6 修改登录密码 (仅适用于这四者)
         syb.unohacha.com/Api/Api/User_verification
         */
        SharedPreferences sp = getSharedPreferences("userinfo",0);
        final String token = sp.getString("token","0");
        if (token.equals("0")){
            MyToast("用户信息获取失败");
            return;
        }
        Log.e("验证验证码", "" + Url.USER_VERIFICATION);
        OkGo.<String>post(Url.USER_VERIFICATION)
                .tag(this)
                .params("mobile", Base64Util.encodedString2(phone))
                .params("code", ed_bindphone2_code.getText().toString())
                .params("type", 4)//3 忘记密码 4 更换手机号(第一次使用,第二次请求更换手机号接口) 5 修改交易密码  6 修改登录密码 (仅适用于这四者)
                .params("token",token)
                .execute(new DialogCallback<String>(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("验证验证码", "" + response.body());
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(response.body().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String res = jsonObject.optString("re");
                        String info = jsonObject.optString("info");
                        if (res.equals("1")) {//验证成功跳转到新手机绑定界面
                            Intent intent = new Intent(BindPhone2Activtity.this,BindPhone3Activity.class);
                            startActivity(intent);
                        } else {
                            MyToast("获取失败");
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        MyToast("获取失败");
                    }
                });
    }


    //    获取验证码
    //    @param Str    code      接口请求密码  UNOHACHA
    //    @param Int    mobile    手机号   （加密）
    //    @param Int    type      1 注册  2 登录  3 忘记密码   4 更换手机号  5 修改交易密码  6 修改登录密码  7三方登陆  8绑定银行卡
    //    syb.unohacha.com/Api/Api/User_getMessage
    private void bindPhonegetcode() {
        Map<String, String> map = new HashMap<>();
        map.put("code", Url.CODE);
        map.put("mobile", Base64Util.encodedString2(phone));
        map.put("type", "4");
        Log.e("获取验证码", "" + Url.REGISTER_GETCODE);
        OkGo.<InfoBean>post(Url.REGISTER_GETCODE)
                .tag(this)
                .params(map, false)
                .execute(new DialogCallback<InfoBean>(this) {
                    @Override
                    public void onSuccess(Response<InfoBean> response) {
                        Log.e("REGISTER_GETCODE", "" + response);
                        if (response.body().getRe().equals("1")) {
                            handler.sendEmptyMessage(100);
                            MyToast(response.body().getInfo());
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<InfoBean> response) {
                        super.onError(response);
                    }
                });
    }


    @Override
    protected void handleMsg(Message msg) {
        switch (msg.what) {
            case 100:
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_bindphone2_getcode, 60000, 1000);
                mCountDownTimerUtils.start();
                break;
        }
    }
}
